package net.adriana.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import net.adriana.Fac;
import net.adriana.bean.Table;
import net.adriana.component.*;

public class SecondWindow extends MyFrame{

	/**
	 * 
	 */
	private static final long serialVersionUID = -222423363806081865L;
	
	private MyList tableList;
	private Table selectedTable = null;
	private Table previousSelectedTable = null;
	private JCheckBox extendsBox;
	private JCheckBox implementsBox;
	private JCheckBox methodsBox;
	private JCheckBox serializableBox;
	private JCheckBox toStringBox;
	private JCheckBox compareToBox;
	private JTextField implementNo1 = new JTextField();
	private JTextField implementNo2 = new JTextField();
	private MyButton addBtn;
	private MyButton next;
	private JTextField extendsText = new JTextField();
	private int col = 0;
	private ArrayList<JTextField> implementsTexts = new ArrayList<>();

	public SecondWindow() {
		super(500, 340);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		addLabel();
		addList();
		addCheckbox();
		addTexts();
		addButton();
		setVisible(true);
	}
	
	public void addCheckbox(){
		int x = 200;
		extendsBox = new JCheckBox(" Extends");
		initCheckBox(extendsBox, x, 35, 80, 30);
		extendsBox.addChangeListener(new ChangeListener() {
			
			@Override
			public void stateChanged(ChangeEvent e) {
				if(extendsBox.isSelected()){
					extendsText.setEditable(true);
				}else extendsText.setEditable(false);
			}
		});
		
		implementsBox = new JCheckBox(" implements");
		initCheckBox(implementsBox, x, 80, 100, 30);
		implementsBox.setSelected(true);
		implementsBox.addChangeListener(new ChangeListener() {
			
			@Override
			public void stateChanged(ChangeEvent e) {
				if(implementsBox.isSelected()){
					serializableBox.setEnabled(true);
//					addBtn.setEnabled(true);
					if(implementsTexts.size()!=0){
						for (JTextField txt : implementsTexts) {
							txt.setEditable(true);
						}
					}
				}else{
					serializableBox.setEnabled(false);
//					addBtn.setEnabled(false);
					if(implementsTexts.size()!=0){
						for (JTextField txt : implementsTexts) {
							txt.setEditable(false);
						}
					}
				}
			}
		});
		
		serializableBox = new JCheckBox(" Serializable");
		initCheckBox(serializableBox, x + 60, 110, 110, 30);
		serializableBox.setSelected(true);
		
		methodsBox = new JCheckBox(" 常用方法");
		initCheckBox(methodsBox, x, 150, 100, 30);
		methodsBox.setSelected(true);
		methodsBox.addChangeListener(new ChangeListener() {
			
			@Override
			public void stateChanged(ChangeEvent e) {
				if(methodsBox.isSelected()){
					toStringBox.setEnabled(true);
					compareToBox.setEnabled(true);
				}else{
					toStringBox.setEnabled(false);
					compareToBox.setEnabled(false);
				}
			}
		});
		
		toStringBox = new JCheckBox(" toString()");
		initCheckBox(toStringBox, x + 60, 180, 100, 30);
		toStringBox.setSelected(true);
		
		compareToBox = new JCheckBox(" compareTo()");
		initCheckBox(compareToBox, x + 170, 180, 100, 30);
	}
	
	private void initCheckBox(JCheckBox box, int x, int y, int width, int height){
		box.setBounds(x, y, width, height);
		box.setFocusPainted(false);
		box.setEnabled(false);
		content.add(box);
	}
	
	private void addList(){
		tableList = new MyList(Fac.schema.getTablesName());
		content.add(tableList);
		tableList.setLocaAndScroll(content,80, 80, 80, 180);
		tableList.addListSelectionListener(new ListSelectionListener() {
			@Override
			public void valueChanged(ListSelectionEvent e) {
				
				int i = tableList.getSelectedIndex();
				if(selectedTable == null){
					selectedTable = Fac.tables[i];
				}else{
					previousSelectedTable = selectedTable;
					selectedTable = Fac.tables[i];
				}
				
				if(e.getValueIsAdjusting()){
					
					if(previousSelectedTable != null) saveDatum(previousSelectedTable);
					
					extendsBox.setEnabled(true);
					implementsBox.setEnabled(true);
					methodsBox.setEnabled(true);
					
					//重置继承
					extendsBox.setSelected(selectedTable.isExtends());
					extendsText.setText(selectedTable.getExtends());
					//重置接口
					implementsBox.setSelected(selectedTable.isSelectedImple());
					serializableBox.setSelected(selectedTable.isImpleContains("Serializable"));
					//TODO more implements need
					
					//重置方法
					methodsBox.setSelected(selectedTable.isSelectedMethods());
					
					toStringBox.setSelected(selectedTable.isSelectedMethods("toString()"));
					compareToBox.setSelected(selectedTable.isSelectedMethods("compareTo()"));

					
					
				}
			}
		});
	}
	private void addButton(){
		addBtn = new MyButton("add");
		addBtn.setBounds(400, 116, 30, 18);
		addBtn.setEnabled(false);
		addBtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if(col == 0){
					initText(implementNo1, 264, 150, 80, 20);
					implementsTexts.add(implementNo1);
				}else if(col == 1){
					initText(implementNo2, 370, 150, 80, 20);
					implementsTexts.add(implementNo2);
				}
				col++;
				setMethodsLocation(20);
			}
		});
		content.add(addBtn);
	}
	
	private void setMethodsLocation(int y){
		methodsBox.setBounds(200, 150 + y, 100, 30);
		toStringBox.setBounds(260, 180 + y, 100, 30);
		compareToBox.setBounds(370, 180 + y, 100, 30);
		repaint();
	}
	
	private void addTexts(){
		initText(extendsText, 300, 42, 100, 20);
		extendsText.setEditable(false);
	}

	private void initText(JTextField text, int x, int y, int width, int height){
		text.setBounds(x, y, width, height);
		content.add(text);
	}
	
	private void addLabel(){
		initLabel("数据库 " + Fac.schema.getSchemaName(), 30, 0, 200, 50);
		initLabel("Table列表", 85, 35, 100, 50);
	}
	
	public void addNextBtn(){
		next = new MyButton("下一步");
		next.setBounds(335,270,55,20);
		next.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				//保存数据
				if(selectedTable != null) saveDatum(selectedTable);
				if(Fac.last == null){
					Fac.last = new LastWindow();
					Fac.currentWindow = Fac.last;
					Fac.last.addPreviousBtn();
					Fac.last.addExitBtn();
				}
				else Fac.last.setVisible(true);
				Fac.currentWindow = Fac.last;
				Fac.second.setVisible(false);
			}
		});
		content.add(next);
	}
	
	private void saveDatum(Table table){
		//保存继承
		if(extendsBox.isSelected())
			table.setExtendsClass(extendsText.getText(), true);
		else table.setExtendsClass(extendsText.getText(), false);
		//保存接口
		if(implementsBox.isSelected()){
			table.setSelectedImple(true);
			//TODO 
			
		}else table.setSelectedImple(false);
			
		if(serializableBox.isSelected()) 
			table.setInterfaceNames("Serializable");
		else table.removeInterface("Serializable");
		
		//保存常用方法
		if(methodsBox.isSelected()){
			table.setSelectedMethods(true);
		}else table.setSelectedMethods(false);
		if(toStringBox.isSelected())
			table.setMethods("toString()", true);
		else table.setMethods("toString()", false);
		if(compareToBox.isSelected())
			table.setMethods("compareTo()", true);
		else table.setMethods("compareTo()", false);
	}
}
